#!/bin/bash
# -----------------------------------------------------------------------------
# 文件：app.sh
# 描述：编译、部署、打包脚本
# 版本：1.0
# 作者：lg
# 日期：2023-11-17
# 修改：2023-11-17 1.0 lg  新增版本
# -----------------------------------------------------------------------------

# ------------ 变量定义 ------------
# 项目
PROJECT=odm
# 环境路径
WORK_DIR=$HOME/work/$PROJECT
# 前置
VIEW=$WORK_DIR/${PROJECT}v
# 后台
SERVE=$WORK_DIR/${PROJECT}s
# 目标路径
DIST=$WORK_DIR/dist
# LIB路径
LIB="$WORK_DIR/lib"
# 备份路径
BACKUP=$WORK_DIR/backup
# 包路径
PACKAGE=$WORK_DIR/package
# ---------------------------------


# 用法
usage(){
  echo "Usage: sh app.sh [args]"
  echo "                 install      -- 安装部署"
  echo "                 build back   -- 编译后台程序"
  echo "                 build front  -- 编译前台程序"
  echo "                 init         -- 初始化"
  echo "                 dev          -- 启动服务[开发]"
  echo "                 serve        -- 启动服务[生产]"
  echo "                 stop         -- 停止服务"
  echo "                 package      -- 打包项目"
}

# 编译前台程序
buildFront(){
  echo "编译前台程序开始..."
  cd $VIEW
  npm run build
  echo "npm run build"

  # dist目录不存在时创建
  if [ ! -d $DIST ];then
    mkdir $DIST
    echo "创建目录:$DIST"
  fi
  
  rm -rf $DIST/static
  echo "rm -rf $DIST/static"
  cp -R $VIEW/dist $DIST
  echo "cp -R $VIEW/dist $DIST"
  mv $DIST/dist $DIST/static
  rm -rf $VIEW/dist
  echo "rm -rf $VIEW/dist"
  cd $WORK_DIR
  echo "编译前台程序成功"
}

# 编译后台程序
buildBack(){
  echo "编译后台程序开始..."
  cd $SERVE
  go build -o $PROJECT
  echo "go build -o $PROJECT"

  # dist目录不存在时创建
  if [ ! -d $DIST ];then
    mkdir $DIST
    echo "创建目录:$DIST"
  fi

  mv ./$PROJECT $DIST
  echo "mv $SERVE/$PROJECT $DIST"
  cp $SERVE/config.yaml $DIST
  echo "cp $SERVE/config.yaml $DIST/config.yaml"
  # dist/lib目录不存在时创建
  if [ ! -d $DIST/lib ];then
    mkdir $DIST/lib
    echo "创建目录:$DIST/lib"
  fi
  # cp $LIB/query.jar $DIST/lib
  # echo "cp $LIB/query.jar $DIST/lib"
  # cp $LIB/command.jar $DIST/lib
  # echo "cp $LIB/command.jar $DIST/lib"
  # cp $LIB/program.jar $DIST/lib
  # echo "cp $LIB/program.jar $DIST/lib" 
  echo "编译后台程序成功"
}

backup(){
  echo "程序备份开始..."
  name=${PROJECT}.$(date +%Y%m%d%H%M%S).tar.gz
  # backup目录不存在时创建
  if [ ! -d $BACKUP ];then
    mkdir $BACKUP
  fi
  cd $WORK_DIR
  
  # dist目录不存在时创建
  if [ ! -d $DIST ];then
    mkdir $DIST
    echo "创建目录:$DIST"
  fi
  
  tar zcvf $BACKUP/$name dist
  echo "tar zcvf $BACKUP/$name $DIST"
  echo "程序备份成功"
}

# 安装部署
install(){
  echo "================== 程序安装部署开始... =================="
  backup
  # 删除dist目录下的文件
  rm -rf $DIST/*
  echo 'rm -rf '$DIST/*
  buildb
  buildf
  echo "================= 程序安装部署成功 ======================="
}

# 检查输入参数的个数
if [ $# -eq 0 ];then
  usage
  exit
fi

case "$1" in
  # ------------------ build ------------
  "build")
  if [ -z $2 ];then
    usage
    exit
  elif [ $2 = "back" ];then
    # 编译后台
    buildBack
    exit
  elif [ $2 = "front" ];then
    # 编译前台
    buildFront
    exit
  else
    usage
    exit
  fi
  ;;
  # ------------------ backup ---------------
  "backup")
  backup
  exit
  ;;
  # ------------------ install ---------------
  "install")
  install
  exit
  ;;
  # ------------------ init ---------------
  "init")
  cd $DIST
  ./$PROJECT init -f config.yaml
  cd $WORK_DIR
  ;;
  # ------------------ dev   ---------------
  "dev")
  cd $DIST
  ./$PROJECT serve -f config.yaml
  cd $WORK_DIR
  ;;
  # ------------------ serve ---------------
  "serve")
  cd $DIST
  nohup ./$PROJECT serve -f config.yaml >/dev/null 2>&1 &
  cd ..
  cd $WORK_DIR
  ;;
  # ------------------ stop ----------------
  "stop")
  cd $DIST
  ./$PROJECT stop
  cd $WORK_DIR
  ;;
  # ------------------ package -------------
  "package")
  name="package.$(date +%Y%m%d%H%M%S).tar.gz"


  # dist目录不存在时创建
  if [ ! -d $PACKAGE ];then
    mkdir $PACKAGE
    echo "创建目录:$PACKAGE"
  fi

  # tar -zcvf package/$name --exclude="dist/$PROJECT" dist doc app.sh ${PROJECT}s ${PROJECT}v
  # 打包把程序打包进入
  tar -zcvf package/$name dist doc app.sh ${PROJECT}s ${PROJECT}v
  ;;
  # ------------------ error ---------------
  *) usage
  ;;
esac
